package cn.edu.ujn.bmis.tools;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import cn.edu.ujn.bmis.service.UserFunctionManager;

import com.opensymphony.xwork2.ActionContext;

@Component("helper")
public class Helper {

	private HibernateTemplate hibernateTemplate;
	private UserFunctionManager userFunctionManager;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource(name = "hibernateTemplate")
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	//功能权限验证
	@SuppressWarnings("unchecked")
	public boolean checkFunction(String functionNum){
		if(functionNum.indexOf(",")!=-1){
			String t[]=functionNum.split(",");
			functionNum=t[0];
		}
		ArrayList<String> functionNumList=new ArrayList<String>();
		Map session = (Map) ActionContext.getContext().getSession();
		functionNumList=(ArrayList)session.get("functionNumList");
		if(functionNumList.contains(functionNum)){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 向前台返回执行结果
	 */
	public static void out(String statusCode, String message,
			String callbackType, String rel, String forwardUrl, String navTabId) {
		JSONObject object = new JSONObject();
		object.put("statusCode", statusCode);
		object.put("message", message);
		object.put("callbackType", callbackType);
		object.put("rel", rel);
		object.put("forwardUrl", forwardUrl);
		object.put("navTabId", navTabId);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		try {
			PrintWriter out = response.getWriter();
			out.print(object);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 返回查询记录
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List find(final String hql) {

//		List<?> list = hibernateTemplate.executeFind(new HibernateCallback() {
//			public Object doInHibernate(Session session)
//					throws HibernateException, SQLException {
//				Query query = session.createQuery(hql);
//				List<?> list = query.list();
//				return list;
//				
//			}
//		});
		List<?>list = null;
		try {
			list = hibernateTemplate.find(hql);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return list;
	}
	
	/**
	 * 返回所有记录数
	 */
	@SuppressWarnings("unchecked")
	public int findSize(final String hql) {
		Number number=null;
		number = hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				return query.uniqueResult();
			}
		});
		return number.intValue();
	}
	
	/**
	 * 
	 * 分页函数
	 */
	@SuppressWarnings("unchecked")
	public List fenYe(final String hql, final int pageNow, final int pageSize) {
		try {
			List list = hibernateTemplate.executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session)throws HibernateException, SQLException {
					Query query = session.createQuery(hql);
					//query.setFirstResult((pageNow - 1) * pageSize); //如果去掉这一行
					//query.setMaxResults(pageSize); //再去掉这一行 可以正常查出来 详见：http://www.debugease.com/javaweb/185475.html
					List list = query.list();
					return list;
				}
			});
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public UserFunctionManager getUserFunctionManager() {
		return userFunctionManager;
	}
	
	@Resource(name="userFunctionManager")
	public void setUserFunctionManager(UserFunctionManager userFunctionManager) {
		this.userFunctionManager = userFunctionManager;
	}
	
	

}
